//加载事件
$(function () {

    //判断是否在本地保存token
    chenggongdenglu();

});

function chenggongdenglu(){
    //判断显示登陆按钮还是显示个人用户按钮
    $("#loginFunction").css("display","none");
    $("#userFunction").css("display","none");
    if(getCookie("token")==""||getCookie("token")==null){
        $("#loginFunction").css("display","block");
        //需要将两个提交按钮类型改为button
        //需要：jQuery
        //需要：/js/cookie.js
        //登陆事件所用的js
        var common={phone:null}
        //发送验证码按钮
        $(".form.form-quick-login .btn.btn-getcode.disabled").click(function(){

            if($(this).html()!="获取验证码")
            {
                layer.msg("获取验证码过于频繁",{time:750});
                return;
            }
            //手机号
            var phone=$(" .form.form-quick-login  .input.input-mobile").val();
            common.phone=phone;
            //验证手机号
            var rex2 = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
            if(!rex2.test(phone)){
                layer.msg("请输入正确的手机号",{time:750});
                return;
            }

            //发送ajax请求
            $.ajax({
                url: 'http://cn.pn.com/api-pn/user/user/p/sendMessage',
                dataType: 'json',
                type: 'post',
                data: {phone:phone,isRegister:false},
                success: function (data) {
                    switch (parseInt(data.errorCode)) {
                        case 0000:
                            var shijian=60;
                            //创建计时器
                            var jishiqivar=window.setInterval(function(){
                                $(".form.form-quick-login .btn.btn-getcode.disabled").html(shijian-=1);
                                if(shijian<0){
                                    $(".form.form-quick-login .btn.btn-getcode.disabled").html("获取验证码");
                                    window.clearInterval(jishiqivar);
                                }
                            },1000);
                            break;
                        case 1001:
                            var shijian=parseInt(data.data);
                            //创建计时器
                            var jishiqivar=window.setInterval(function(){
                                $(".form.form-quick-login .btn.btn-getcode.disabled").html(shijian-=1);
                                if(shijian<0){
                                    $(".form.form-quick-login .btn.btn-getcode.disabled").html("获取验证码");
                                    window.clearInterval(jishiqivar);
                                }
                            },1000);
                            break;
                        case 1002:
                            layer.msg("您在短时间内获取过多验证码，请检查手机号是否可用，请在一小时后再次尝试");
                            break;
                        case 1003:
                            layer.msg("你是在企图什么？？禁用js?????愤怒！！");
                            break;
                        case 1004:
                            break;
                    }
                    layer.msg(data.msg,{time:650});


                },
                error: function (response) {
                    console.log(response);
                }
            });
        });
        //快速登陆
        $(".form.form-quick-login .btn.btn-submit").click(function(){
            var phone=$(".form.form-quick-login  .input.input-mobile").val();
            var YZM=$(".form.form-quick-login  .input.input-getcode").val();
            if(YZM==null || YZM==""){
                layer.msg("请输入验证码",{time:650});
                return;
            }
            if(common.phone==""||common.phone==null){
                layer.msg("请先获取验证码",{time:750});
                return;
            }
            if(phone!=common.phone){
                layer.msg("前后手机号不一致",{time:750});
                return;
            }

            //发送ajax请求
            $.ajax({
                url: 'http://cn.pn.com/api-pn/user/user/p/fastLogin',
                dataType: 'json',
                type: 'post',
                data: {phone:phone,YZM:YZM},
                success: function (data) {
                    switch (parseInt(data.errorCode)) {
                        case 0000:
                            setCookie("token",data.data.token,1);
                            chenggongdenglu();
                            break;
                        case 1003:
                            break;
                        case 1005:
                            break;
                        case 1006:
                            break;
                    }
                    layer.msg(data.msg,{time:650});

                },
                error: function (response) {
                    console.log(response);
                }
            });
        });
        //账号密码登陆
        $(".form.form-login .btn.btn-submit").click(function(){
            var phone=$(".form.form-login  .input.input-mobile").val();
            var userPwd=$(".form.form-login  .input.input-password").val();
            if(phone==""||phone==null||userPwd==""||userPwd==null){
                layer.msg("请输入账号和密码",{time:750});
                return;
            }

            //发送ajax请求
            $.ajax({
                url: 'http://cn.pn.com/api-pn/user/user/p/login',
                dataType: 'json',
                type: 'post',
                data: {telephone:phone,userPwd:userPwd},
                success: function (data) {
                    switch (parseInt(data.errorCode)) {
                        case 0000:
                            //设置成功
                            setCookie("token",data.data.token,1);
                            chenggongdenglu();
                            break;
                        case 1007:
                            break;
                        case 1008:
                            break;
                    }
                    layer.msg(data.msg,{time:650});

                },
                error: function (response) {
                    console.log(response);
                }
            });
        });
    }
    else{//已经登陆
        $("#userFunction").css("display","block");
        User.getUserInfo.execution();
        var data=User.getUserInfo.getResult();
        $("#userFunction img").attr("src",data.data.headImgURL);

        //鼠标移入移出事件
        $('#userFunction .login').mouseenter(function(){
            $('#userFunction .abcdefg').css({'display':'block'})
        });
        $('#userFunction .abcdefg').mouseenter(function(){
            $('#userFunction .abcdefg').css({'display':'block'})
        });
        $('#userFunction .abcdefg').mouseleave(function(){
            $('#userFunction .abcdefg').css({'display':'none'})
        });
        $('#userFunction .login').mouseleave(function(){
            $('#userFunction .abcdefg').css({'display':'none'})
        });
        //我的订单
        $("#userFunction ul.submenu-inner li").eq(0).click(function (e) {
            location.href="http://localhost:8080/static/html/one.html";
        });
        //我的钱包
        $("#userFunction ul.submenu-inner li").eq(1).click(function (e) {
            location.href="http://localhost:8080/static/html/discountcoupon.html";
        });
        //账户设置
        $("#userFunction ul.submenu-inner li").eq(2).click(function (e) {
            location.href="http://localhost:8080/static/html/message.html";
        });
        //退出登陆
        $("#userFunction ul.submenu-inner .logout").click(function (e) {
            User.logout.execution();
            var data=User.logout.getResult();
            switch (parseInt(data.errorCode)) {
                case 0000:
                    removeCookie("token");
                    chenggongdenglu();
                    break;
                case 100001:

                    break;
            }
            layer.msg(data.msg);
        })

    }
}